function ProjectInfoOffline() {
    var _this = this;
    var page;
    _this.page_size = 10;

    // init
    _this.init = function(_page) {
        page = _page;
        // _this.initTable();
        _this.initEvent();
    }

    _this.initTable = function() {
        var list_url = appPath + "/admin/projectOffline/userKaoqinList";
        page.init("select_user_form", list_url, "select_user_list", "select_user_page", 1, _this.page_size);
        page.goPage(1);
        page.list = function(dataList){
            _this.obj = dataList;
            if(dataList == null){
                return;
            }
            var len = dataList.length;
            var inner = "", item;
            // 组装数据
            for(var i=0; i< len; i++) {
                item = dataList[i];
                // 组装数据
                inner += "<tr>";
                inner += "<td>"+(parseInt(i)+1)+"</td>";
                inner += '<td width="50"><input type=\"checkbox\" name="userBox" value="' + item['userId'] + '"></td>';
                inner += "<td>"+item.userName+"</td>";
                inner += "<td>"+item.companyName+"</td>";
                if(item.departmentName == null){
                    inner += "<td>/</td>";
                }else{
                    inner += "<td>"+item.departmentName+"</td>";
                }
                inner += "</tr>";
            }
            return inner;
        }

    };

    _this.initEvent = function () {
        //搜索
        $("#user_search").click(function () {
            _this.initTable();
        });

        //全部
        $("#user_all").click(function () {
            $("#userName").val("");
            _this.initTable();
        });

        // 全选
        $("#checkAll").click(function () {
            _this.userCheckAll();
        });

    }

    // 全选
    _this.userCheckAll=function(){
        var flag=$("#checkAll").prop("checked");
        $('#select_user_list input[type="checkbox"]').prop("checked",flag);
    }
    
    // 选择人员
    _this.selectUser = function () {
        // 调用选择培训人员弹窗
        layer.open({
            type : 2,
            title : '选择培训人员',
            area : [ '1200px', '80%' ],		//弹出层大小
            scrollbar : false,				//false隐藏滑动块
            content : [ appPath + '/popup/enterSelectUser', 'yes' ]
        });
    }

    // 选择人员后回调(保存线下项目人员)
    _this.train_user_callback = function (rows) {
        var users = [];
        for(var i=0; i<rows.length; i++){
            if(rows[i].id){
                var obj=new Object();
                obj.id=rows[i].id;
                obj.userName=rows[i].userName;
                obj.userAccount=rows[i].userAccount;
                obj.companyId=rows[i].companyId;
                obj.companyName=rows[i].companyName;
                obj.deptName=rows[i].departmentName==null?"":rows[i].departmentName;
                obj.departmentId=rows[i].departmentId==null?rows[i].companyId:rows[i].departmentId;
                users.push(obj);
            }
        }
        users =JSON.stringify(users);
        // 保存项目人员
        $.ajax({
            url: appPath + '/admin/projectOffline/save_project_user',
            async: false,
            type: 'post',
            data: {
                'projectId': $("#projectId").val(),     // 项目id
                'users': users,
            },
            success: function(data){
                var code = data['code'];
                if(code==10000){
                    layer.alert('操作成功', {icon: 1,  skin: 'layer-ext-moon',closeBtn:0}, function(index){
                        layer.close(index);
                        // 刷新项目人员
                        _this.initTable();
                    });
                }else{
                    layer.alert('操作失敗', {icon: 2,  skin: 'layer-ext-moon',closeBtn:0});
                }
            }
        });
    }

    // 批量删除
    _this.deleteBatch = function () {
        var userIds=[]
        $('#select_user_list input:checked').each(function(i,e){
            userIds.push($(this).val());
        });
        if(userIds.length==0){
            layer.msg("未选择人员");return;
        }
        layer.confirm("确定此操作吗?", {
            icon : 3,
            btn : [ "确认", "取消" ]
        }, function() {
            $.ajax({
                url: appPath + '/admin/projectOffline/deleteProjectUser',
                async: false,
                type: 'post',
                data: {
                    'projectId': $("#projectId").val(), 	            // 项目总览id
                    'ids': userIds.join(",")	                        // 人员ids
                },
                success: function(data){
                    var code = data['code'];
                    if(code==10000){
                        layer.alert('操作成功', {icon: 1,  skin: 'layer-ext-moon'}, function(index){
                            layer.close(index);
                            _this.initTable();
                        });
                    }else{
                        layer.alert('操作失敗', {icon: 2,  skin: 'layer-ext-moon'});
                    }
                }
            });
        });
    }
    _this.save = function () {
        $.ajax({
            url: appPath + '/admin/projectOffline/saveOfflineProject',
            async: false,
            type: 'POST',
            dataType:"json",
            data: $("#project_offline_form").serialize(),
            success: function(data){
                var code = data['code'];
                if(code==10000){
                    // layer.alert('保存成功', {icon: 1,  skin: 'layer-ext-moon', closeBtn:0}, function(index){
                    //     layer.close(index);
                        // 刷新项目人员
                        // _this.initTable();
                    // });
                    window.location.href=appPath+"/admin/projectOffline/index";
                }else{
                    layer.alert('保存失敗', {icon: 2,  skin: 'layer-ext-moon'});
                }
            }
        });
    }

    // 返回
    _this.closeWindow = function () {
        window.history.go(-1);
    }
}

var projectInfoOffline = new ProjectInfoOffline();